/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import model.DatabaseCommunicator;

/**
 *
 * @author Jeff
 * 
 * Frame for pop-up window after addition of new Drink to database
 * 	to fulfill need for Producer. If Produced typed exists, add
 * 	link between drink and producer to "beerstobrewery"
 * 	"winestowinery" or "spirittodistillery" tables. If doesn't
 * 	exist, create new producers with "unknown" for all fields
 *  besides name, then create link between drink and producer.
 * 
 */
public class ProducerFrame extends javax.swing.JFrame {
	
	// Variables
	public static DatabaseCommunicator dbc;

    /**
     * Creates new form ProducerFrame
     */
    public ProducerFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        submitButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Please enter the producer name below.");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        submitButton.setText("Submit");
        submitButton.addActionListener (new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	String tableName = "";
                String producerName = jTextField1.getText();
                String specificValues = "NA";
                if ((dbc.currAddType).equals("beer")){
            		tableName = "brewery";
            		int microMacro = 0;
            		specificValues = String.valueOf(microMacro);
            		
            	} else if ((dbc.currAddType).equals("wine")){
            		tableName = "winery";
            		int currentProduction = 0;
            		int acres = 0;
            		String wineMaker = "";
            		specificValues = currentProduction +"','"+ acres +"','"+
            							wineMaker;
            		
            	} else if ((dbc.currAddType).equals("spirit")){
            		tableName = "distillery";
            		String founder = "";
            		specificValues = founder;
            		
            	} else {
            		System.out.println("Should never reach here" + dbc.currAddType);
            	}
            	
        		String stmt = "INSERT IGNORE INTO "+ tableName + " VALUES (" +"'"+ producerName +
        				"','0','','"+ 
        				specificValues + "')";
        		dbc.create(stmt);

        		
        		System.out.println(producerName);
        		dbc.currProducer = producerName;
        		//dbc.currAddType = null;
        		
        		String targetTable = "fat";
        		if ((dbc.currAddType).equals("beer")){
        			targetTable = "beerstobrewery";
        		} else if ((dbc.currAddType).equals("wine")){
        			targetTable = "winestowinery";
        		} else if ((dbc.currAddType).equals("spirit")){
        			targetTable = "spiritstodistillery";
        		} else {
        			System.out.println("ERROR NewEntryFrame, Line 294");
        		}
        		
        		stmt = "INSERT INTO "+targetTable+ " VALUES ('"+dbc.currDrink+"','"+dbc.currProducer+"')";
        		System.out.println(stmt);
        		dbc.create(stmt);
        		
        		dispose();
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener (new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	dispose();
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(submitButton)
                                .addGap(18, 18, 18)
                                .addComponent(cancelButton)))
                        .addGap(0, 46, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(submitButton)
                    .addComponent(cancelButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ProducerFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ProducerFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ProducerFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ProducerFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ProducerFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton submitButton;
    // End of variables declaration
}
